package BackUpAction;

import java.io.File;

import java.util.ArrayList;


import Bean.NewsBean;
import Utils.DbAccess;
import Utils.GenUtils;

public class DeleteOldData{
	public static ArrayList<String> bookmark = new ArrayList<String>();
	
	
	
	public static void main(String[] args) {
		loadBookmark();
		for(int k=0; k < 10; k++){
			ArrayList<NewsBean> arr = new DeleteOldData().getNews(args[0]);
			try{Thread.sleep(2000);}catch (Exception e) {}
			for(int i=0; i < arr.size(); i++){
				System.out.println(arr.get(i).getNewsId());
				String newsId = arr.get(i).getNewsId();
				boolean proccess=true;
				for(int j=0; j < bookmark.size(); j++){
					if(bookmark.get(j).indexOf("-"+newsId+"-")>=0){
						proccess=false;
						break;
					}
				}
				if(proccess){
					String thumbnail = arr.get(i).getThumbnail();
					String homepic = arr.get(i).getHomePic();
					String picture = arr.get(i).getPicture();
					
					removeFromBookmark(newsId);
					if(removeNews(newsId)){
						deleteFile(newsId, thumbnail);
						deleteFile(newsId, homepic);
						deleteFile(newsId, picture);
					}
					System.out.println("=========");
				}else{
					System.out.println("Co bookmark "+newsId);
					//try{Thread.sleep(2000);}catch (Exception e) {}
				}
			}
		}
	}
	
	
	public static void loadBookmark(){
		DbAccess db = new DbAccess();
		String sql = "SELECT bookmark FROM bookmark";			
		try{
			db.connectTemp("localhost", Utils.Constant.DB);
			db.stmt = db.conn.prepareStatement(sql);
			db.rs = db.stmt.executeQuery();
			while(db.rs.next()){
				bookmark.add(db.rs.getString("bookmark"));
				System.out.println(db.rs.getString("bookmark"));
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			db.DBClose();
		}
	}
	
	public ArrayList<NewsBean> getNews(String date){
		DbAccess db = new DbAccess();
		String sql = "SELECT NewsId,Thumbnail,HomePic,Picture,DatePublished FROM news_old " +
			"WHERE DatePublished < ? LIMIT 500";			
		ArrayList<NewsBean> re = new ArrayList<NewsBean>();
		try{
			db.connectTemp("localhost", Utils.Constant.DB);
			db.stmt = db.conn.prepareStatement(sql);
			db.stmt.setString(1, date);
			db.rs = db.stmt.executeQuery();
			while(db.rs.next()){
				NewsBean bean = new NewsBean();
				bean.setNewsId(db.rs.getString("NewsId"));
				bean.setThumbnail(db.rs.getString("Thumbnail"));
				bean.setHomePic(db.rs.getString("HomePic"));
				bean.setPicture(db.rs.getString("Picture"));
				System.out.println(db.rs.getString("DatePublished"));
				re.add(bean);
			}
			return re;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			db.DBClose();
		}
	}
	public static boolean removeFromBookmark(String newsId){
		DbAccess db = new DbAccess();
		String sql = "UPDATE bookmark set bookmark = REPLACE(bookmark,'-"+newsId+"-','')";			
		
		try{
			db.connectTemp("localhost", Utils.Constant.DB);
			db.stmt = db.conn.prepareStatement(sql);
			System.out.println("Update Bookmark");
			db.stmt.executeUpdate();
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			db.DBClose();
		}
		return true;
	}
	public static boolean removeNews(String newsId){
		DbAccess db = new DbAccess();
		String sql = "DELETE FROM news_old where NewsId=?";			
		try{
			db.connectTemp("localhost", Utils.Constant.DB);
			db.stmt = db.conn.prepareStatement(sql);
			db.stmt.setString(1, newsId);
			if(db.stmt.executeUpdate()>=0){
				System.out.println("Xoa tin: "+newsId);
				db.stmt.close();
				sql = "DELETE FROM news_cat_old WHERE NewsId=?";
				db.stmt = db.conn.prepareStatement(sql);
				db.stmt.setString(1, newsId);
				if(db.stmt.executeUpdate()>=0) {
					System.out.println("Xoa cat: "+newsId);
					return true;
				}
				else return false;
			}
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			db.DBClose();
		}
		return true;
	}
	public static boolean deleteFile(String newsId, String listFile){
		try{
			String []arr = listFile.split(",");
			for(int i=0; i < arr.length; i++){
				String source = GenUtils.genPathByAccountId(newsId)+"/"+arr[i];
				if(new File(source).exists()){
					System.out.println("===>Xoa file "+source);
					new File(source).delete();
				}
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
		return true;
	}
}